<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:custom="http://www.w3.org/1999/xhtml">
<head>
    <title>后台管理系统</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" th:href="@{|/bootstrap-4.6.2/css/bootstrap.min.css|}">
    <script th:src="@{../jquery/jquery-3.6.0.min.js}"></script>
    <script th:src="@{../bootstrap-4.6.2/js/bootstrap.min.js}"></script>
    <link th:href="@{|../css/addEvent.css|}" rel="stylesheet" type="text/css" media="all" />
</head>
<body>
<div class="box">
    <div class="title">
        <h2>添加事件</h2>
    </div>
</div>
<div class="envent" id="envent">
    <button  class="btn  btn-primary"  data-toggle="modal" data-target="#exampleModal" onclick="getGrade()">添加事件</button>
</div>
<div  style="margin: 20px 40px">
    <div class="btnList">
        <ul>
            <li><button onclick="getEventList()"  class="btn btn-primary">投票开始事件</button></li>
            <li> <button onclick="getEventList2()"  class="btn btn-primary">投票未开始事件</button></li>
            <li> <button  onclick="getEventList3()"  class="btn btn-primary">资金筹集统计</button></li>
            <li> <button  onclick="getIncomeList()"  class="btn btn-primary">收入清单</button></li>
            <li> <button  onclick="getOutEventList()"  class="btn btn-primary">支出清单</button></li>
            <li><a  class="btn btn-primary" href="/logout">退出登陆</a></li>
        </ul>


    </div>
        <table  class="table table-hover">
            <tbody id="eventList">

            </tbody>
        </table>
    

</div>


<!-- Modal 模态框-->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true" th:fragment="pageInfo">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">班费筹集事件添加</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <form >
                    <div class="form-group">
                        <label for="eName">事件名称:</label>
                        <input type="text" class="form-control"  placeholder="" id="eName">
                    </div>
                    <div class="form-group">
                        <label for="epay">预算筹集</label>
                        <input type="number" class="form-control" id="epay" placeholder="">
                    </div>
                    <div class="form-group">
                        <label for="grade">年级:</label>
                        <select class="form-control" id="grade" th:onchange="getClazz(this.value)">
                        </select>

                    </div>
                    <div class="form-group">
                        <label for="clazz">班级:</label>
                        <select class="form-control" id="clazz">
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="sDate">开始时间</label>
                        <input type="datetime-local" class="form-control" id="sDate" placeholder="">
                    </div>
                    <div class="form-group">
                        <label for="eDate">结束时间</label>
                        <input type="datetime-local" class="form-control" id="eDate" placeholder="">
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button"  onclick="addEvent()" class="btn btn-primary">添加</button>
            </div>
        </div>
    </div>
</div>
<script>
    getEventList2();
    getGrade();
    function getIncomeList() {
        $.ajax({
            url: "/student/getIncomeList",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    $("#eventList").append(" <tr> <td>" +
                        "收入事由</td> <td>" +
                        "收入金额</td> <td>" +
                        "收入时间</td></tr>");
                    for (let i = 0; i < json.data.length; i++) {
                        const event = json.data[i];
                        $("#eventList").append(
                            "<tr> <td>" + event.in_name +
                            "</td> <td>" + event.in_money+
                            "</td> <td>" + event.in_date +
                            "</td> </tr>");
                    }
                } else {
                    alert(json.msg);
                }
            }
        });

    }
    function getOutEventList() {
        $.ajax({
            url: "/student/getOutEventList",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    $("#eventList").append(" <tr> <td>" +
                        "支出事由</td> <td>" +
                        "支出金额</td> <td>" +
                        "支出时间</td></tr>");
                    for (let i = 0; i < json.data.length; i++) {
                        const event = json.data[i];
                        $("#eventList").append(
                            "<tr> <td>" + event.pay_name +
                            "</td> <td>" + event.pay_money+
                            "</td> <td>" + event.pay_date +
                            "</td> </tr>");
                    }
                } else {
                    alert(json.msg);
                }
            }
        });
        
    }
    function getGrade() {
        $.ajax({
            url: "/event/getGrade",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#grade").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const grade = json.data[i];
                        $("#grade").append("<option value="+grade.gid+">" + grade.gname + "</option>");
                    }
                }
            }
        });
 

    }
    function getClazz(gid) {
        $.ajax({
            url: "event/getClazz",
            type: "POST",
            data:{
                gid:gid
            },
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#clazz").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const clazz = json.data[i];
                        $("#clazz").append("<option >" + clazz.cName + "</option>");
                    }
                }
            }
        });
    }
    function addEvent() {
        let eName=  $("#eName").val();
        let ePay=$("#epay").val();
        let sDate= $("#sDate").val();
        let eDate= $("#eDate").val();
        let grade= $("#grade").val();
        let clazz= $("#clazz").val();
        console.log(sDate);
        console.log(eDate);
        $.ajax({
            url: "/event/add",
            type: "POST",
            data:{
                ename:eName,
                epay:ePay,
                sdate:sDate,
                edate:eDate,
                grade:grade,
                clazz:clazz,
            },
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $('#exampleModal').modal('hide')
                    location.reload();
                    $("#grade").empty();
                    $("#clazz").empty();

                }
            }
        });
    }
    function getEventList(){
        $.ajax({
            url: "/event/selectEventList",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        let isStart= "";
                        let isOk="";
                        const event= json.data[i];
                        if (event.isDoing===1){
                            isStart="投票已开始";
                        }else  {
                            isStart="投票未开始";
                        }
                        if (event.isOk===0){
                            isOk="不成立";
                        }else {
                            isOk="成立";
                        }
                        console.log(event.isDoing);
                        $("#eventList").append("<tr><td>" +event.eName+
                            "</td><td>" +event.ePay+
                            "</td><td>" +event.eNum+
                            "</td><td>" +event.sTime+
                            "</td><td>" +event.eTime+
                            "</td><td>" +isOk+
                            "</td><td>" +isStart+
                            "</td><td>" +event.supNum+
                            "</td><td>" +event.noSup+
                            "</td><td>" +event.cid+
                            "</td><td>" +event.totalPay+
                            "</td><td>" +"<button disabled onclick='beginSupport("+event.eid+")' class='btn btn-primary'>开始投票</button>"+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
    function getEventList2(){
        $.ajax({
            url: "/event/selectEventList2",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        let isStart= "";
                        let isOk="";
                        const event= json.data[i];
                        if (event.isDoing===1){
                            isStart="投票已开始";
                        }else  {
                            isStart="投票未开始";
                        }
                        if (event.isOk===0){
                            isOk="不成立";
                        }else {
                            isOk="成立";
                        }
                        console.log(event.isDoing);
                        $("#eventList").append("<tr><td>" +event.eName+
                            "</td><td>" +event.ePay+
                            "</td><td>" +event.eNum+
                            "</td><td>" +event.sTime+
                            "</td><td>" +event.eTime+
                            "</td><td>" +isOk+
                            "</td><td>" +isStart+
                            "</td><td>" +event.supNum+
                            "</td><td>" +event.noSup+
                            "</td><td>" +event.cid+
                            "</td><td>" +event.totalPay+
                            "</td><td>" +"<button  onclick='beginSupport("+event.eid+")' class='btn btn-primary'>开始投票</button>"+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
    function getEventList3(){
        $.ajax({
            url: "/event/getIsOkEvent",
            type: "POST",
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const event= json.data[i];
                        let estatus="";
                        if (event.totalPay>=event.ePay){
                            estatus="已完成筹集!"
                        }else {
                            estatus="正在筹集中!"
                        }
                        $("#eventList").append("<tr><td>" +event.eName+
                            "</td><td>" +event.eName+
                            "</td><td>" +event.totalPay+
                            "</td><td>" +estatus+
                            "</td><td>" +event.eTime+
                            "</td><td>" +"<button  onclick='getEventList4("+event.eid+")' class='btn btn-primary'>查看筹集资金清单</button>"+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
    function getEventList4(eid){
        $.ajax({
            url: "/event/getEventInfoStu",
            type: "POST",
            data:{eid:eid},
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    $("#eventList").empty();
                    for (let i = 0; i < json.data.length; i++) {
                        const event= json.data[i];
                        $("#eventList").append(
                            "<tr><td>" +event.e_name+
                            "</td><td>" +event.s_name+
                            "</td><td>" +event.pay+
                            "</td><td>" +event.pdate+
                            "</td></tr>");
                    }
                }else {
                    alert(json.msg);
                }
            }
        });
    }
    function  beginSupport(eid){
        $.ajax({
            url: "/event/supportStart",
            type: "POST",
            data:{eid:eid},
            dataType: "json",
            success: function (json) {
                if (json.code == 200) {
                    alert(json.msg);
                    location.reload();
                }else {
                    alert(json.msg);
                }
            }
        });
    }

</script>
</body>
</html>
